package org.douxiaoban.client.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.douxiaoban.client.R;
import org.douxiaoban.client.api.DoubanApi;
import org.douxiaoban.client.app.App;
import org.douxiaoban.data.DouGroupOneTopicData.DouReplyItemData;
import org.douxiaoban.data.DoubanBaseData;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class DouGroupOneTopicAdapter extends DoubanAdapter{

//	public ArrayList<DouReplyItemData> mListData;
	private Map<String, Bitmap> mUrlBitmap;
	private LayoutInflater mInflater;
	public DouGroupOneTopicAdapter(Context context,
			List<? extends DoubanBaseData> list) {
		super(context, list);
		mUrlBitmap = new HashMap<String, Bitmap>(); 
	}
	
	public Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg){
			Bundle bundle = msg.getData();
			String str = bundle.getString(DoubanApi.KEY_URL);
			Bitmap bitmap = (Bitmap)msg.obj;
			mUrlBitmap.put(str, bitmap);
			notifyDataSetChanged();
		}
	};
	
	public void recycleAll(){
		for(String key : mUrlBitmap.keySet()){
			mUrlBitmap.get(key).recycle();
		}
		mUrlBitmap.clear();
	}

	@Override
	public View getView(int position, View view, ViewGroup vGroup) {
		DouReplyItemData item = (DouReplyItemData)mListData.get(position);
		mInflater = LayoutInflater.from(mContext);
		HandleView handleView = null;
		if(null == view){
			handleView = new HandleView();
			view = mInflater.inflate(R.layout.group_one_topic_info, null);
			handleView.mAvatarIv = (ImageView)view.findViewById(R.id.topic_avatar);
			handleView.mSignureTv = (TextView)view.findViewById(R.id.topic_signature);
			handleView.mContentTv = (TextView)view.findViewById(R.id.topic_content);
			handleView.mReContentTv = (TextView)view.findViewById(R.id.topic_reply_content);
			handleView.mTimeTv = (TextView)view.findViewById(R.id.topic_time);
			handleView.mNameTv = (TextView)view.findViewById(R.id.topic_name);
			handleView.mReplyLy = view.findViewById(R.id.topic_reply_content_ly);
			
		}else{
			handleView = (HandleView)view.getTag();
		}
		Bitmap bitmap = mUrlBitmap.get(item.mAvatarLink);
		if(null == bitmap){
			App.getDouanApiInstance().reqBitmap(mHandler, item.mAvatarLink);
		}else{
			handleView.mAvatarIv.setImageBitmap(bitmap);
		}
		handleView.mNameTv.setText(item.mName);
		handleView.mContentTv.setText(Html.fromHtml(item.mContent));
		if(item.mReplyQuote != null && item.mReplyQuote.length() > 0){
			handleView.mReplyLy.setVisibility(View.VISIBLE);
			
			handleView.mReContentTv.setText(Html.fromHtml(item.mReplyQuote));
		}else{
			handleView.mReplyLy.setVisibility(View.GONE);
		}
		if(null == item.mSignature || item.mSignature.length() == 0){
			handleView.mSignureTv.setVisibility(View.GONE);
		}else{
			handleView.mSignureTv.setVisibility(View.VISIBLE);
			handleView.mSignureTv.setText(item.mSignature);
		}
		handleView.mTimeTv.setText(item.mTime);
		view.setTag(handleView);
		return view;
	}
	
	private static class HandleView{
		TextView mNameTv;
		ImageView mAvatarIv;
		TextView mTimeTv;
		TextView mSignureTv;
		TextView mContentTv;
		TextView mReContentTv;
		View mReplyLy;
	}
	
}